import { Module } from 'vuex'
import { IRootState } from '../types'
import { IDrivesState, IModuleDrives } from './types'
import {
  updateModuleDrives,
  getModuleDrives
} from '@/render/service/drives/drives'

const drivesModule: Module<IDrivesState, IRootState> = {
  namespaced: true,
  state() {
    return {
      moduleDrives: new Map<string, IModuleDrives>()
    }
  },
  mutations: {
    SET_MODULEDRIVES(state, { projectName, projectModuleName, val }) {
      if (state.moduleDrives == null) {
        state.moduleDrives = new Map()
      }
      state.moduleDrives.set(`${projectName}_${projectModuleName}`, val)
    }
  },
  actions: {
    loadModuleDrives({ commit, state }, value) {
      return new Promise((resolve, reject) => {
        if (
          state.moduleDrives &&
          state.moduleDrives.size > 0 &&
          state.moduleDrives.has(
            `${value.projectName}_${value.projectModuleName}`
          )
        ) {
          resolve(
            state.moduleDrives.get(
              `${value.projectName}_${value.projectModuleName}`
            )
          )
        } else {
          getModuleDrives(value.projectName, value.projectModuleName)
            .then((res: any) => {
              const data = res.data
              const moduleDrives = data
              commit('SET_MODULEDRIVES', {
                projectName: value.projectName,
                projectModuleName: value.projectModuleName,
                val: moduleDrives
              })
              resolve(moduleDrives)
            })
            .catch((error: any) => {
              reject(error)
            })
        }
      })
    },
    updateModuleDrives({ commit, state }, moduleDrives) {
      return new Promise((resolve, reject) => {
        updateModuleDrives(moduleDrives)
          .then((res: any) => {
            commit('SET_MODULEDRIVES', {
              projectName: moduleDrives.projectName,
              projectModuleName: moduleDrives.projectModuleName,
              val: moduleDrives
            })
            resolve(res)
          })
          .catch((error: any) => {
            reject(error)
          })
      })
    },
    addApiConfigDef(
      { commit, state },
      { projectName, projectModuleName, data }
    ) {
      return new Promise((resolve, reject) => {
        if (state.moduleDrives !== null) {
          console.log(data)
          const moduleDrives = state.moduleDrives.get(
            `${projectName}_${projectModuleName}`
          )
          if (moduleDrives != null) {
            moduleDrives?.configDatas.push({
              name: data.name,
              value: data.value,
              type: 'api',
              dataType: undefined,
              definitionRef: undefined
            })
            state.moduleDrives.set(
              `${projectName}_${projectModuleName}`,
              moduleDrives
            )
            console.log(
              state.moduleDrives.get(`${projectName}_${projectModuleName}`)
            )
            resolve('添加成功')
          } else {
            reject('添加失败')
          }
        } else {
          reject('添加失败')
        }
      })
    }
  }
}

export default drivesModule
